import { waitCheck, classList, getEdit, selectList, setScore } from '../../api/waitCheck'
import { Message } from 'view-design'
import router from '../../router'
const state = {
  // 待批班级数据
  checkInfo: {
    data: []
  },
  // 详情页
  getdata: {},
  // 待批学生
  classLists: {
    data: []
  },
  // 分页
  pageData: [],
  // 班级下拉数据
  SelectList: []
}
const mutations = {
  // 待批班级数据
  getcheckInfo(state, payload) {
    state.checkInfo.data = payload
  },
  // 待批学生
  getClassList(state, getList) {
    // console.log(getList,'--------------')
    getList.data && getList.data.forEach(item => {
      item.grade_name = getList.name
    })
    state.classLists.data = getList.data
    console.log(getList, '------船只')
  },
  // 分页
  getPage(state, data) {
    const start = (data.pagenum - 1) * data.pagesize
    const end = start + data.pagesize
    state.pageData = state.checkInfo.data.slice(start, end)
    console.log(state.checkInfo.data)
  },
  // 班级下拉数据
  getSelect(state, selects) {
    state.SelectList = selects
    console.log(state.SelectList)
  },
  // 详情页
  getdata(state, data) {
    state.getdata = data
  }
}
const actions = {
  // 待批班级
  async getcheckList({ commit }) {
    const { data: res } = await waitCheck()
    console.log(res)
    if (res.code !== 1) {
      Message.error(res.msg)
    }
    console.log(res)

    commit('getcheckInfo', res.data)
    commit('getPage', { pagesize: 3, pagenum: 1 })
  },
  // 待批学生
  async getclassInfo({ commit }, action) {
    console.log(action, '-------action')
    const { data: res } = await classList({
      grade_id: action.id
    })
    if (res.code !== 1) {
      Message.error(res.msg)
    }
    // 添加班级名
    commit('getClassList', { data: res.exam, name: action.className })
  },
  // 班级下拉框
  async getSelectInfo({ commit }) {
    const { data: res } = await selectList()
    console.log(res)
    if (res.code !== 1) {
      Message.error(res.msg)
    }
    commit('getSelect', res.data)
  },
  // 试卷详情数据
  async editInfo({ commit }) {
    const { data: res } = await getEdit()
    if (res.code !== 1) {
      Message.error(res.msg)
    }
    console.log(res, '-------------试卷')
    commit('getdata', res.data)
  },
  // 批卷接口
  async setScoreInfo({ dispatch }, data) {
    const { data: res } = await setScore(data)
    if (res.code !== 1) {
      return Message.error(res.msg)
    }
    Message.success(res.msg)
    router.push({ name: 'awaitclassmeta' })
    dispatch('editInfo')
  }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}
